//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for InkWebControl.
	/// </summary>
	public class InkWebControl : CommunityServer.Controls.TemplatedWebControl
	{
		public InkWebControl()
		{
			
		}

        

		public bool CanUserInk
		{
			get
			{
				CSContext context = CSContext.Current;
				return context.SiteSettings.EnableInk && context.User.EnableInk;
			}
		}

		const string undefined = "undefined";

		protected override void AttachChildControls()
		{
			hidden = FindControl("HiddenInk") as HtmlInputHidden;


			if(CanUserInk)
			{
				StringBuilder sb = new StringBuilder();

				sb.Append("<object id=\"" + this.ClientID + "\" height=\"480\" width=\"600\" ");
				sb.AppendFormat(" classid={0}#InkWeb.InkArea",Globals.GetSiteUrls().InkDll);
				sb.Append(" ViewAsText=\"ViewAsText\"></object>");

				Literal objectTag = FindControl("objecttag") as Literal;
				objectTag.Text = sb.ToString();
			}
		}

		private string _BackgroundImageURL = null;

		public string BackgroundImageURL
		{
			get
			{
				if(_BackgroundImageURL == null && Context.Request.QueryString["IB"] != null)
					_BackgroundImageURL = Context.Request.QueryString["IB"];

				return _BackgroundImageURL;
			}
			set { _BackgroundImageURL = value;}
		}


		System.Web.UI.HtmlControls.HtmlInputHidden hidden = null;

		public string InkData
		{
			get
			{
				this.EnsureChildControls();
                if(Globals.IsNullorEmpty(hidden.Value) || hidden.Value == undefined) 
                    return null;
                else
				    return  hidden.Value;
			}
			set
			{
				this.EnsureChildControls();
				hidden.Value = value;
			}
		}


		protected override void OnPreRender(EventArgs e)
		{
			if(this.Enabled && CanUserInk)
			{
				StringBuilder sb = new StringBuilder();
				sb.Append("<script language=\"javascript\" >");

				//sb.AppendFormat("function {0}_Load()",this.ClientID);
				//sb.Append("{");

				sb.AppendFormat("var ic = document.getElementById('{0}');",this.ClientID);
				sb.AppendFormat("var hc = document.getElementById('{0}');",this.hidden.ClientID);
	
				sb.Append("if(ic != null) { if(ic.InkEnabled){");
				sb.Append("ic.InkData = hc.value;");

				if(!Globals.IsNullorEmpty(BackgroundImageURL))
				{
                    sb.AppendFormat("ic.BackgroundImageStyle = 2;");
					sb.AppendFormat("ic.BackgroundImageURL = '{0}';",this.BackgroundImageURL);
				}
			
				sb.Append("}else{ic.style.display = 'none';}}");
	
				//sb.Append("}");
				sb.Append("\n\n");
				sb.AppendFormat("function {0}_Save()",this.ClientID);
				sb.Append("{");

                sb.Append("try {");
				sb.AppendFormat("	var inkControl = document.getElementById('{0}');",this.ClientID);
				sb.AppendFormat("	var hiddenControl = document.getElementById('{0}');", this.hidden.ClientID);

				if(Globals.IsNullorEmpty(BackgroundImageURL))
				{
					sb.Append("	hiddenControl.value =inkControl.InkData;");
				}
				else
				{
					//CombinedImageData
					sb.Append("	hiddenControl.value =inkControl.InkWithEmbeddedCombinedImageInkString;");
				}
                sb.Append("} catch(e){ }");

 

                sb.Append("}");



				sb.Append("</script>");

                this.Page.RegisterStartupScript(this.ClientID+"submit",sb.ToString());
				this.Page.RegisterOnSubmitStatement(this.ClientID+"submit" , string.Format("{0}_Save();",this.ClientID));
				
			}

			base.OnPreRender (e);
		}

        private string _inkValidation = null;
        public string InkValidationFunction
        {
            get{ return _inkValidation;}
            set{_inkValidation = value;}
        }

        public void RegisterLinkButton(LinkButton lb)
        {
            string save = string.Format("{0}_Save();",this.ClientID);
            string text = lb.Attributes["onclick"];
            if(!Globals.IsNullorEmpty(text))
            {
                save = save + text;
            }
            lb.Attributes.Add("onclick", save);

        }







	}
}
